package com.tanhua.manage.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.manage.pojo.PublishInfo;
import com.tanhua.manage.pojo.UserInfo;
import com.tanhua.manage.vo.Pager;
import com.tanhua.manage.vo.PublishItemsVo;
import com.tanhua.manage.vo.TotalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.transform.Source;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author guzi
 * @date 2021/1/12
 */
@Service
public class PublishAuditService {
    public static final String PASS = "1";
    public static final String REJECT = "2";
    public static final String REVOCATION = "3";

    @Autowired
    private PublishInfoService publishInfoService;

    @Autowired
    private UserInfoService userInfoService;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    public Pager<PublishItemsVo> getPublishAuditList(Map<String, Object> param) {

        Pager<PublishItemsVo> pager = new Pager<>();
        List<PublishItemsVo> publishItemsVoList = new ArrayList<>();

        //查询 动态 相关信息
        IPage<PublishInfo> publishInfoIPage = this.publishInfoService.queryPublishInfoListByParam(param);
        List<PublishInfo> publishInfoList = publishInfoIPage.getRecords();

        //如果没有数据就返回空pager
        if (null == publishInfoIPage) {
            return pager;
        }

        //获取 所有用户ID和 PublishId
        List<Long> userIds = new ArrayList<>();
        List<String> publishIds = new ArrayList<>();
        for (PublishInfo publishInfo : publishInfoList) {
            publishIds.add(publishInfo.getPublishId());
            if (!userIds.contains(publishInfo.getUserId())) {
                userIds.add(publishInfo.getUserId());
            }
        }

        //构造查询条件，查询所有用户
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.in("user_Id", userIds);
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(wrapper);
        System.out.println(userInfoList);


        //调用 Dubbo 查询动态具体信息   ！！！没有数据！！！
        //List<Publish> publishes = this.quanZiApi.queryPublishByPublishIds(publishIds);

        //生成模拟数据
        String text = "作为一个高手，我习惯了孤独！";
        String[] media = new String[]{"https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/6/CL-3.jpg",
                "https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/5/1564567248169.jpg",
                "https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/5/1564567255147.jpg"};

        //两层 For 循环填充数据
        for (PublishInfo publishInfo : publishInfoList) {
            for (UserInfo userInfo : userInfoList) {
                if (publishInfo.getUserId().longValue() == userInfo.getUserId().longValue()) {

                    PublishItemsVo publishItemsVo = new PublishItemsVo();
                    publishItemsVo.setId(publishInfo.getPublishId());
                    publishItemsVo.setNickname(userInfo.getNickName());
                    publishItemsVo.setUserId(userInfo.getUserId().intValue());
                    publishItemsVo.setUserLogo(userInfo.getLogo());
                    publishItemsVo.setCreateDate(publishInfo.getCreateDate());
                    publishItemsVo.setText(text);
                    publishItemsVo.setState(publishInfo.getState());
                    publishItemsVo.setReportCount(publishInfo.getReportCount());
                    publishItemsVo.setLikeCount(publishInfo.getLikeCount());
                    publishItemsVo.setCommentCount(publishInfo.getCommentCount());
                    publishItemsVo.setForwardingCount(publishInfo.getForwardingCount());
                    publishItemsVo.setMedias(media);
                    publishItemsVoList.add(publishItemsVo);
                }
            }
        }

        //设置total
        List<TotalVo> list = new ArrayList<>();

        //查询数量
        Integer total1 = this.publishInfoService.queryPublishByState("all");
        Integer total2 = this.publishInfoService.queryPublishByState("3");
        Integer total3 = this.publishInfoService.queryPublishByState("5");
        Integer total4 = this.publishInfoService.queryPublishByState("4");

        this.publishInfoService.queryPublishByState("all");
        TotalVo totalVo1 = new TotalVo();
        totalVo1.setTitle("全部");
        totalVo1.setValue(total1);
        totalVo1.setCode("all");
        list.add(totalVo1);

        TotalVo totalVo2 = new TotalVo();
        totalVo2.setTitle("待审核");
        totalVo2.setValue(total2);
        totalVo2.setCode("3");
        list.add(totalVo2);

        TotalVo totalVo3 = new TotalVo();
        totalVo3.setTitle("已通过");
        totalVo3.setValue(total3);
        totalVo3.setCode("5");
        list.add(totalVo3);

        TotalVo totalVo4 = new TotalVo();
        totalVo4.setTitle("已拒绝");
        totalVo4.setValue(total4);
        totalVo4.setCode("4");
        list.add(totalVo4);

        pager.setTotals(list);

        pager.setCounts(publishInfoIPage.getTotal());
        pager.setPage(publishInfoIPage.getCurrent());
        pager.setPageSize(publishInfoIPage.getSize());
        pager.setPages(publishInfoIPage.getPages());
        pager.setItems(publishItemsVoList);

        //TODO 条数范围限制

        return pager;
    }


    public String publishPass(String[] publishIds) {
        return this.publishInfoService.publishAuditByPublishIdsAndState(publishIds, PASS);
    }

    public String publishReject(String[] publishIds) {
        return this.publishInfoService.publishAuditByPublishIdsAndState(publishIds, REJECT);
    }

    public String publishRevocation(String[] publishIds) {
        return this.publishInfoService.publishAuditByPublishIdsAndState(publishIds, REVOCATION);
    }
}
